package com.tsing.service.impl;

import com.tsing.common.result.PageResult;
import com.tsing.mapper.ArticleMapper;
import com.tsing.mapper.CollectMapper;
import com.tsing.mapper.DynamicMapper;
import com.tsing.mapper.LoveMapper;
import com.tsing.mapper.impl.ArticleMapperImpl;
import com.tsing.mapper.impl.CollectMapperImpl;
import com.tsing.mapper.impl.DynamicMapperImpl;
import com.tsing.mapper.impl.LoveMapperImpl;
import com.tsing.pojo.entity.TlLove;
import com.tsing.pojo.entity.TlPost;
import com.tsing.pojo.entity.TlUser;
import com.tsing.pojo.vo.ArticleDetailsVO;
import com.tsing.pojo.vo.PostDetailsVO;
import com.tsing.service.ArticleService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴阳基
 * @datetime 2023/12/8 11:35
 */
public class ArticleServiceImpl implements ArticleService {
    private final ArticleMapper articleMapper = new ArticleMapperImpl();

    private final LoveMapper loveMapper = new LoveMapperImpl();

    private final CollectMapper collectMapper = new CollectMapperImpl();

    private final DynamicMapper dynamicMapper = new DynamicMapperImpl();

    @Override
    public  PageResult<ArticleDetailsVO> list(Integer channelId,Integer page,Integer size,Long userId) {

        PageResult<ArticleDetailsVO> apArticleVOList = new PageResult<>();



        List<ArticleDetailsVO> list = new ArrayList<>();

        PageResult<TlPost> pageResultList = articleMapper.list(channelId,page,size);


        for (TlPost tlPost : pageResultList.getData()) {

            ArticleDetailsVO a = new ArticleDetailsVO();

            a.setStatus(tlPost.getStatus());
            a.setPostId(tlPost.getPostId());
            a.setImage(tlPost.getImage());
            a.setTitle(tlPost.getTitle());
            a.setLikeCount(tlPost.getLikeCount());
            //根据用户id查找对应的用户数据
            List<TlUser> tlUsers = articleMapper.userList(tlPost.getUserId());
            //获取点赞数
            List<TlLove> tlLove = loveMapper.loveCount(userId,tlPost.getPostId());
            a.setLoveCount((long) tlLove.size());

            for (TlUser tlUser : tlUsers) {
                a.setUserName(tlUser.getUsername());
                a.setUserImage(tlUser.getUserImage());
            }
            list.add(a);

        }
        apArticleVOList.setData(list);
        apArticleVOList.setCode(pageResultList.getCode());
        apArticleVOList.setSize(pageResultList.getSize());
        apArticleVOList.setTotal(pageResultList.getTotal());
        apArticleVOList.setCurrentPage(pageResultList.getCurrentPage());
        return apArticleVOList;
    }

    @Override
    public Integer likeCountAdd(Integer postId, Integer likeCount) {
        return articleMapper.likeCountAdd(postId,likeCount);
    }

    @Override
    public Integer likeCountDelete(Integer postId, Integer likeCount) {
        return articleMapper.likeCountDelete(postId,likeCount);
    }

    @Override
    public Integer add(Integer userId, String title, String image, String createDate, String address ,
                       String Copywriting, Long channelId) {
        return articleMapper.add(userId,title,image,createDate,address,Copywriting,channelId);
    }

    @Override
    public List<PostDetailsVO> postDetails(Integer postId, Integer userId) {
        PostDetailsVO postDetailsVO = new PostDetailsVO();
        List<PostDetailsVO> postDetailsList = new ArrayList<>();
        TlPost post = articleMapper.getPostById(postId);
        TlUser user = articleMapper.getUserById(Integer.parseInt(post.getUserId().toString()));
        Boolean flag = articleMapper.getFlag(Integer.parseInt(post.getUserId().toString()), userId) > 0;

        //查询用户是否点赞该作品
        List<TlLove> tlLoves = loveMapper.loveCount(Long.parseLong(userId.toString()), Long.parseLong(postId.toString()));
        //查询该作品的收藏数
        Integer collectCount = dynamicMapper.collectCount(Long.parseLong(postId.toString()));
        //查询当前用户是否收藏该作品
        Integer i = collectMapper.collectCount(userId, Long.parseLong(postId.toString()));

        postDetailsVO.setPostId(Long.parseLong(postId.toString()));
        postDetailsVO.setIfCollect(i>0);
        postDetailsVO.setCollectCount(collectCount);
        postDetailsVO.setIfLove(!tlLoves.isEmpty());
        postDetailsVO.setAddress(post.getAddress());
        postDetailsVO.setCopywriting(post.getCopywriting());
        postDetailsVO.setDetailsId(post.getDetailsId());
        postDetailsVO.setFlag(flag);
        postDetailsVO.setChannelId(post.getChannelId());
        postDetailsVO.setCreateDate(post.getCreateDate());
        postDetailsVO.setUserId(post.getUserId());
        postDetailsVO.setImage(post.getImage());
        postDetailsVO.setTitle(post.getTitle());
        postDetailsVO.setLikeCount(post.getLikeCount());
        postDetailsVO.setUserImage(user.getUserImage());
        postDetailsVO.setUserName(user.getUsername());
        postDetailsList.add(postDetailsVO);
        return postDetailsList;
    }
}
